package com.difsonapps.android.marker.adapters;

import com.difsonapps.android.marker.FeedImageView;
import com.difsonapps.android.marker.R;
import com.difsonapps.android.marker.app.AppController;
import com.difsonapps.android.marker.beans.Album;
import com.difsonapps.android.marker.util.MarkerUtil;
 
import java.util.List;
 
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
 
public class AlbumGridAdapter extends BaseAdapter {  
    private Activity activity;
    private LayoutInflater inflater;
    private List<Album> albums;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    private boolean modePhotos = true;
    public static final int MAX_ALBUM_TITLE_LENGHT = 13;
    int screenWidth;
    int colBasWidth = 100;
    int newWidth = 100;
    //int screenHeight;
 
    public AlbumGridAdapter(Activity activity, List<Album> albums) {
        this.activity = activity;
        this.albums = albums;
        
        Display display = activity.getWindowManager().getDefaultDisplay(); 
        screenWidth = display.getWidth();
        
        int nbCols = screenWidth/colBasWidth;
        if(nbCols*colBasWidth>screenWidth) nbCols = nbCols - 1;
        newWidth = screenWidth/nbCols;
        if(newWidth*nbCols>screenWidth) newWidth--;
        newWidth = newWidth - 1;
        Log.d("Numbers of cols", ""+nbCols);
        Log.d("New width", ""+newWidth);
        //screenHeight = display.getHeight();
    }
    
    public int getNewWidth(){
    	return newWidth;
    }
    
    
 
    public boolean isModePhotos() {
		return modePhotos;
	}
	public void setModePhotos(boolean modePhotos) {
		this.modePhotos = modePhotos;
	}



	@Override
    public int getCount() {
        return albums.size();
    }
 
    @Override
    public Object getItem(int location) {
        return albums.get(location);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
 
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.album_item, null);
 
        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
 
        ImageView facebookIcon = (ImageView) convertView.findViewById(R.id.facebookIcon);
        TextView name = (TextView) convertView.findViewById(R.id.album_name);
        TextView timestamp = (TextView) convertView
                .findViewById(R.id.timestamp);
        TextView nbPicts = (TextView) convertView
                .findViewById(R.id.nbPics);
        NetworkImageView albumPic = (NetworkImageView) convertView.findViewById(R.id.albumPic);
        //FeedImageView feedImageView = (FeedImageView) convertView.findViewById(R.id.feedImage1);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) albumPic.getLayoutParams();
        params.width = newWidth;
        params.height = newWidth;
        albumPic.setLayoutParams(params);
        
        Album item = albums.get(position);
 
        facebookIcon.setVisibility(item.isFacebook()?View.VISIBLE:View.GONE);
        name.setText(MarkerUtil.abbreviate(item.getName(), MAX_ALBUM_TITLE_LENGHT));
 
        // Converting timestamp into x ago format
        CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
                Long.parseLong(item.getTimeStamp()),
                System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
        timestamp.setText(timeAgo);
 
        // Chcek for empty status message
        if(modePhotos){
	        if (item.getCount_photos()>0) {
	        	String compl = "";//this.activity.getString(item.getCount_photos()==1?R.string.picture:R.string.pictures);
	        	nbPicts.setText(""+item.getCount_photos()+" "+compl);
	        	nbPicts.setVisibility(View.VISIBLE);
	        } else {
	        	//nbPicts.setText(R.string.no_picture);
	        	nbPicts.setVisibility(View.GONE);
	        }
        }else{
        	if (item.getCount_videos()>0) {
	        	String compl = "";//this.activity.getString(item.getCount_videos()==1?R.string.video:R.string.videos);
	        	nbPicts.setText(""+item.getCount_videos()+" "+compl);
	        	nbPicts.setVisibility(View.VISIBLE);
	        } else {
	        	//nbPicts.setText(R.string.no_video);
	        	nbPicts.setVisibility(View.GONE);
	        }
        }
 
        // user profile pic
        albumPic.setImageUrl(item.getImage(), imageLoader);
 
        return convertView;
    }
 
}
